/*  
 *  ------ [802_05a] - send packets using a unicast 64-bit address -------- 
 *  
 *  Explanation: This program shows how to send packets with 
 *  XBee-802.15.4 modules. The destination address is set as a
 *  64-bit network address 
 *  
 *  Copyright (C) 2012 Libelium Comunicaciones Distribuidas S.L. 
 *  http://www.libelium.com 
 *  
 *  This program is free software: you can redistribute it and/or modify 
 *  it under the terms of the GNU General Public License as published by 
 *  the Free Software Foundation, either version 3 of the License, or 
 *  (at your option) any later version. 
 *  
 *  This program is distributed in the hope that it will be useful, 
 *  but WITHOUT ANY WARRANTY; without even the implied warranty of 
 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the 
 *  GNU General Public License for more details. 
 *  
 *  You should have received a copy of the GNU General Public License 
 *  along with this program.  If not, see <http://www.gnu.org/licenses/>. 
 *  
 *  Version:           0.1 
 *  Design:            David Gascón 
 *  Implementation:    Yuri Carmona
 */


#include <MakeSureXBee802.h>
#include <MakeSureFrame.h>

// create packet to send
packetXBee* packet; 

char* MoteID="MakeSure_Xbee";
char* MAC_ADDRESS="0013A20040A96860";
char* MAC_LOW="40A96860";
String s;
uint8_t a;


void setup()
{  
  // init Sertial port
	
	Serial.begin(9600);
	Serial.print("802_05a example");
  // init XBee 
  xbee802.ON(); 
    
}


void loop()
{  
  ///////////
   //1. Create Data Frame
  ///////////
   
  //////Continous Switching////////

	for(int i=1;i<5;i++)
   {
	frame.createFrame(ASCII,MAC_LOW,MoteID);
	/*frame.addSensor(SENSOR_STR, "AA"); */
  int value;
 
 	   value=i;
   ////1.1 Create new frame
  switch(value)
  {
	 
    case 1:frame.addSensor(SENSOR_STR, "AA"); 
		break;

	case 2:frame.addSensor(SENSOR_STR, "BB"); 
		break;

	case 3:frame.addSensor(SENSOR_STR, "CC"); 
		break;

	case 4: frame.addSensor(SENSOR_STR, "DD");
	  break;
	
  }
   ///////////
   /////2. Send packet
  ///////////
  
   /////2.1. set packet to send:
  packet=(packetXBee*) calloc(1,sizeof(packetXBee)); // memory allocation
  packet->mode=UNICAST; // set Unicast mode

   /////2.2. sets Destination parameters
  xbee802.setDestinationParams(packet, MAC_ADDRESS, frame.buffer, frame.length, MAC_TYPE); 
  s=String(packet->data);
  Serial.print(s);

    
   ///2.3. send data
  xbee802.sendXBee(packet); 
   ///2.4. Check TX flag
  if( !xbee802.error_TX )
   Serial.println("ok");
  else 
   Serial.println("error"); 

  /// 2.5. free memory
  free(packet);
  packet=NULL; 

  delay(3000);
   }
   }

